package com.devplatform.controller;

import com.devplatform.pojo.vo.DataVo;
import com.devplatform.util.OssUtil;
import com.devplatform.util.UserContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/file")
public class FileController {

    @PostMapping
    public DataVo<String> upload(@RequestParam("file") MultipartFile file) {
        String url = "";
        Long userId = UserContextHolder.getUserId();
        try {
            url = OssUtil.uploadFile(userId, file);
        } catch (IOException e) {
            e.printStackTrace();
            return DataVo.error("文件上传失败");
        }
        return DataVo.ok(url);
    }

    @DeleteMapping("/{fileName}")
    public DataVo<String> delete(@PathVariable String fileName) {
        Long userId = UserContextHolder.getUserId();
        boolean f = OssUtil.deleteFile(userId, fileName);
        return f ? DataVo.ok("删除成功") : DataVo.error("文件不存在");
    }

}
